package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.ProductDao;
import com.itheima.domain.store.Product;
import com.itheima.factory.DaoInstanceFactory;
import com.itheima.service.store.ProductService;

import java.util.List;
import java.util.UUID;

/**
 * @Author: My.zheng
 * @Description:
 * @Date: Created in 22:06  2020/11/29
 * @ModifiedBy:
 */
public class ProductServiceImpl implements ProductService {

    @Override
    public void save(Product product) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        String s = UUID.randomUUID().toString().replace("-","").toUpperCase();
        product.setId(s);

        productDao.save(product);
    }

    @Override
    public void delete(Product product) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        productDao.delete(product);
    }

    @Override
    public void update(Product product) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        productDao.update(product);
    }

    @Override
    public List<Product> findAll() {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        List<Product> productList = productDao.findAll();
        return productList;
    }

    @Override
    public Product findById(String Id) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        Product product = productDao.findById(Id);
        return product;
    }

    @Override
    public PageInfo findAll(int pageNum, int pageSize) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        PageHelper.startPage(pageNum, pageSize);
        List<Product> productList = productDao.findAll();
        return new PageInfo<>(productList);
    }

    @Override
    public List<Product> FuzzyEnquiryTime(String time) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        List<Product> productList = productDao.FuzzyEnquiryTime(time);
        return productList;
    }
    @Override
    public void deleteById(List<String> productIds) {
        // 遍历数组
        if (productIds != null) {
            for (String id : productIds) {
                DaoInstanceFactory.getMapper(ProductDao.class).deleteById(id);
            }
        }
    }

    @Override
    public void dopen(String id) {

        try {
            DaoInstanceFactory.getMapper(ProductDao.class).dopen(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void paralyse(String id) {
        try {
            DaoInstanceFactory.getMapper(ProductDao.class).paralyse(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//    @Override
//    public void paralyse(String id) {
//        try {
//            DaoInstanceFactory.getMapper(ProductDao.class).paralyse(id);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
